package controller;

import com.alibaba.druid.sql.dialect.presto.parser.PrestoLexer;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.Result;
import common.ResultCodeEnum;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import pojo.SysUser;
import service.SysUserService;
import service.impl.SysUserServiceImpl;
import util.MD5Util;
import util.WebUtil;

import javax.management.ObjectName;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ： CodeWater
 * @create ：2022-04-05-16:05
 * @Function Description ：
 *
 * 增加日程的请求
 * 删除日程的请求
 * 修改日程的请求
 * 查询日程的请求
 */
@WebServlet("/user/*")
public class SysUserController extends BaseController {
    private SysUserService userService = new SysUserServiceImpl();
    /**
     * 校验用户名是否存在
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void checkUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //接收用户名
        String username = req.getParameter("username");
        //调用业务层方法
        SysUser user = userService.findByUsername(username);
        //返回结果
        Result result = Result.ok(null);
       //判断
        if(user!=null){
            //用户名已存在
            result = Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        //将result对象转换为JSON数据响应给前端
        WebUtil.writeValue(resp,result);
    }
    /**
     * 注册 接收用户注册请求的业务处理方法(业务接口，不是Java中的接口)
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接收前端提交的json串，转换为user对象，获取信息
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
        SysUser sysUser = WebUtil.readJson(req, SysUser.class);
//        String username = sysUser.getUsername();
//        String password = sysUser.getUserPwd();
//
//        //封装参数
//        SysUser user = new SysUser(null,username, password);
        //调用业务层方法
        int rows =userService.regist(sysUser);
//        //处理结果   不需要使用了我们已将前端项目部署在前端服务器
//        if(rows>0){
//            resp.sendRedirect("/registerSuccess.html");
//        }else {
//            resp.sendRedirect("/registerFail.html");
//        }
        Result result = Result.ok(null);
        if(rows<1){
            result = Result.build(null,ResultCodeEnum.USERNAME_USED);

        }
        WebUtil.writeValue(resp,result);

    }
    /**
     * 登录 登录请求的业务处理方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //获取参数
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
        SysUser sysUser = WebUtil.readJson(req, SysUser.class);
//        //封装参数
//        SysUser user = new SysUser(null,username, password);

        //调用业务层方法
        SysUser loginuser  = userService. findByUsername(sysUser.getUsername());
        //处理结果
//        if(loginuser==null){
//            resp.sendRedirect("/loginUsernameError.html");
//        } else if (!MD5Util.encrypt(sysUser.getUserPwd()).equals(loginuser.getUserPwd())) {
//            resp.sendRedirect("/loginPasswordError.html");
//        }
//        //登录成功
//        else{
//            //登录成功，将用户信息保存到session中
//            HttpSession session = req.getSession();
//            session.setAttribute("loginuser",loginuser);
//            //跳转到首页
//            resp.sendRedirect("/showSchedule.html");
//        }

        Result result =null;
        if(loginuser==null){
            result = Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }
        else if (!MD5Util.encrypt(sysUser.getUserPwd()).equals(loginuser.getUserPwd())) {
            result = Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        else {
            //登录成功 将用户的uid和username放到客户端
            Map map = new HashMap();
            loginuser.setUserPwd(null);
            map.put("loginUser",loginuser);
            result = Result.ok(map);
        }
        WebUtil.writeValue(resp,result);

    }

}
